package com.jdd.dao.controller;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jdd.common.api.vo.Result;
import com.jdd.common.util.LoginUserUtils;
import com.jdd.common.util.ResultGenerator;
import com.jdd.dao.entity.TBuilding;
import com.jdd.dao.entity.TUnit;
import com.jdd.dao.service.ITBuildingService;
import com.jdd.dao.service.ITUnitService;
import com.jdd.dao.service.TAreaService;
import com.jdd.dao.vo.BuildingSerachVo;
import com.jdd.dao.vo.BuildingVo;
import com.jdd.dao.vo.SpaceVo;
import com.jdd.modules.device.entity.TDevice;
import com.jdd.modules.device.service.TDeviceService;
import com.jdd.modules.people.model.TProjectTreeModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 楼栋单元表 前端控制器
 * </p>
 *
 * @author ls
 * @since 2022-09-19
 */
@Slf4j
@RestController("TBuildingController")
@RequestMapping("/building")
public class TBuildingController {

    @Resource
    private ITBuildingService tBuildingService;
    @Resource
    private TAreaService areaService;
    @Resource
    private ITUnitService unitService;
    @Resource
    private TDeviceService deviceService;

    @GetMapping("/buildingVoList")
    public Result<IPage<BuildingVo>> buildingVoList(BuildingSerachVo buildingSerachVo, @RequestParam(name = "pageNo",defaultValue = "1")Integer pageNo,@RequestParam(name = "pageSize",defaultValue = "10")Integer pageSize){
        log.info("buildingSerachVo:{}",buildingSerachVo);
        List<String> projectCodes = LoginUserUtils.getLoginUser().getProjectCodes();
        Result<IPage<BuildingVo>> result = new Result<IPage<BuildingVo>>();
        if (projectCodes.size()<=0){
            result.setSuccess(true);
            result.setResult(null);
            return result;
        }
        Page<BuildingVo> page = new Page<BuildingVo>(pageNo,pageSize);
        IPage<BuildingVo> pageList = tBuildingService.getBuildingInfoPage(page,buildingSerachVo,projectCodes);
        result.setResult(pageList);
        result.setSuccess(true);
        return  result;
    }
    @PostMapping("/changeName")
    public Result changeName(@RequestBody TBuilding tBuilding) {
        tBuilding.setBuildingName(tBuilding.getName());
        TBuilding building = tBuildingService.getById(tBuilding.getId());
        LambdaQueryWrapper<TBuilding> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TBuilding::getAreaId,building.getAreaId());
        queryWrapper.eq(TBuilding::getBuildingName,tBuilding.getName());
        int count = tBuildingService.count(queryWrapper);
        if (count>0){
            return ResultGenerator.genFailResult("修改名称已经存在");
        }
        String deviceParentCode = building.getParentCode()+ "B" + building.getCode();
        deviceService.updateDeviceAlias(deviceParentCode,1,building.getParentCode(),tBuilding.getName());
        tBuildingService.saveOrUpdate(tBuilding);

        return ResultGenerator.genSuccessResult(tBuilding);
    }

    @DeleteMapping("/delete")
    public Result deleteBuilding(@RequestParam(name = "id",required = true)String id){
        LambdaQueryWrapper<TUnit> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TUnit::getBuildingId,id);
        int count = unitService.count(queryWrapper);
        LambdaQueryWrapper<TDevice> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(TDevice::getBuildingId,id);
        int count1 = unitService.count(queryWrapper);
        if (count>0){
            return ResultGenerator.genFailResult("当前楼栋下绑定有单元");
        }
        if (count1>0){
            return ResultGenerator.genFailResult("当前楼栋下绑定有设备");
        }
        tBuildingService.removeById(id);
        return ResultGenerator.genSuccessResult("删除成功");
    }

    @GetMapping("/getBuildList")
    public Result<List<TBuilding>> getBuildList(@RequestParam("areaId") String areaId){
        Result<List<TBuilding>> result = new Result<>() ;
        List<TBuilding> buildingList = new ArrayList<>();
        LambdaQueryWrapper<TBuilding> wrapper = new LambdaQueryWrapper();
        wrapper.eq(TBuilding::getAreaId,areaId);
        buildingList = tBuildingService.list(wrapper);
        result.setResult(buildingList);
        result.setSuccess(true);
        return result;
    }

    @PostMapping(value = "/add")
    public Result<?> addNew(@RequestBody SpaceVo space) {
        return tBuildingService.addNew(space);
    }

    /*传入项目id，返回该项目id下的子节点*/
    @GetMapping("/getChildren")
    public Result<List<TProjectTreeModel>> getChildren(@RequestParam(name = "id")String id){
        List<TProjectTreeModel> tProjectTreeModels = new ArrayList<>();
        LambdaQueryWrapper<TUnit> tAreaLambdaQueryWrapper = new LambdaQueryWrapper<>();
        tAreaLambdaQueryWrapper.eq(TUnit::getBuildingId,id);
        List<TUnit> unitList = unitService.list(tAreaLambdaQueryWrapper);
        for (TUnit tUnit : unitList) {
            tProjectTreeModels.add(new TProjectTreeModel(tUnit));
        }
        return ResultGenerator.genSuccessResult(tProjectTreeModels);
    }
    /*传入项目id，返回该项目id下的子节点*/
    @GetMapping("/getChildrenWithDevice")
    public Result<List<TProjectTreeModel>> getChildrenWithTDevice(@RequestParam(name = "id")String id){
        List<TProjectTreeModel> tProjectTreeModels = new ArrayList<>();
        LambdaQueryWrapper<TUnit> tAreaLambdaQueryWrapper = new LambdaQueryWrapper<>();
        TBuilding building = tBuildingService.getById(id);
        tAreaLambdaQueryWrapper.eq(TUnit::getBuildingId,id);
        List<TUnit> unitList = unitService.list(tAreaLambdaQueryWrapper);
        LambdaQueryWrapper<TDevice> deviceLambdaQueryWrapper = new LambdaQueryWrapper<>();
        deviceLambdaQueryWrapper.eq(TDevice::getBuildingId,id);
        deviceLambdaQueryWrapper.isNull(TDevice::getUnitId).or().eq(TDevice::getUnitId,"");
        List<TDevice> deviceList = deviceService.list(deviceLambdaQueryWrapper);
        for (TDevice device : deviceList) {
            tProjectTreeModels.add(new TProjectTreeModel(device,building));
        }
        for (TUnit tUnit : unitList) {

            tProjectTreeModels.add(new TProjectTreeModel(tUnit));
        }
        return ResultGenerator.genSuccessResult(tProjectTreeModels);
    }

}
